﻿using daonaite.Model;
using daonaite.Model.system.login;
using daonaite.Model.system.user;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;

namespace daonaite.api.Controllers
{
    public class BaseController : ControllerBase
    {
        public IActionResult Success()
        {
            return Ok(new ApiResult<int?>() { Code = 200, Data = null, Message=null });
        }

        public IActionResult Success(string message)
        {
            return Ok(new ApiResult<int?>() { Code = 200, Message = message ,Data = null});
        }
        public IActionResult Success<T>(string message, T data)
        {
            return Ok(new ApiResult<T>() { Code = 200, Message = message, Data = data });
        }

        public IActionResult Success<T>(T data)
        {
            return Ok(new ApiResult<T>() { Code = 200, Data = data });
        }

        public IActionResult Success<T>(T data, int _pagesize,int _pageindex,int _total)
        {
            return Ok(new ApiResult<T>() { Code = 200, Data = data, pageindex = _pageindex, pageSize = _pagesize, totalcount = _total });
        }

        public IActionResult Error(string message)
        {
            return Ok(new ApiResult<int?>() { Code = 400, Message = message ,Data = null });
        }

        /// <summary>
        /// 获取登录用户信息
        /// </summary>
        /// <returns></returns>
        public Loginuser? getloginuser()
        {
            var content = Request.Headers["loginuser"];
            if (content.Count > 0)
            {
                return JsonSerializer.Deserialize<Loginuser>(content);
            }

            return null;
        }

        /// <summary>
        /// 复制值
        /// </summary>
        /// <typeparam name="T">要复制的类</typeparam>
        /// <typeparam name="T1">被复制的类</typeparam>
        /// <param name="data"></param>
        /// <param name="data1"></param>
        public void copy<T, T1>(T data, T1 data1, bool add = false, bool update = false)
        {
            Loginuser user = getloginuser();

            foreach (var item1 in data1.GetType().GetProperties())
            {
                var temp = data.GetType().GetProperties().FirstOrDefault(x => x.Name == item1.Name);
                if (temp != null)
                {
                    if (temp.GetValue(data) != null)
                    {
                        item1.SetValue(data1, temp.GetValue(data));
                    }
                }

                if (add && item1.Name == "create_time")
                {
                    item1.SetValue(data1, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                }
                if (add && item1.Name == "create_user")
                {
                    item1.SetValue(data1, user.user.id);
                }
                if (update && item1.Name == "update_time")
                {
                    item1.SetValue(data1, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                }
                if (update && item1.Name == "update_user")
                {
                    item1.SetValue(data1, user.user.id);
                }
                if (item1.Name == "delete_flag")
                {
                    item1.SetValue(data1, "N");
                }
            }
        }
    }
}
